package com.snopy.lambda;

import com.snopy.pojo.Person;

import java.util.*;

import java.util.function.BiFunction;
import java.util.function.Supplier;
import java.util.function.ToIntBiFunction;

/**
 * @author snopy
 * @version 1.0.0
 * @classname MethodReferencesExamples
 * @description
 * @email 77912204@qq.com
 * @date 2022/07/22 22:14
 */
public class MethodReferencesExamples {

    public static <T> T mergerThings(T a, T b, BiFunction<T,T,T> merger){
      return   merger.apply(a,b);
    }
    public static <T> int compareTwoThings(T a, T b, ToIntBiFunction<T,T> merger){
        return   merger.applyAsInt(a,b);
    }

    public static String appendStrings(String a,String b){
        return a + b;
    }
    public String appendString2(String a,String b){
        return a + b;
    }

    public static void main(String[] args) {
        MethodReferencesExamples myApp = new MethodReferencesExamples();
        String s = MethodReferencesExamples.mergerThings("Hello", "World", MethodReferencesExamples::appendStrings);
        System.out.println(s);
        String s1 = MethodReferencesExamples.mergerThings("Good","Night",myApp::appendString2);
        System.out.println(s1);
        String s2 = MethodReferencesExamples.mergerThings("Hello","World",String::concat);
        System.out.println(s2);
        int s3 = MethodReferencesExamples.compareTwoThings("Hello", "World", String::compareToIgnoreCase);
        System.out.println(s3);

        String[] stringArray = new String[]{"Barbara","James","Mary","John","Patricia","Robert","Michael","Linda"};
        Arrays.sort(stringArray,String::compareToIgnoreCase);
        for (String value : stringArray) {
            System.out.println(value);
        }

        List<Person> roster = Person.createPerson(10);
        //references a constructor
        Set<Person> set = transferElement(roster, HashSet::new);
        set.forEach(Person::printPerson);
    }

    public static <T,Source extends Collection<T>,Dest extends Collection<T>> Dest transferElement(Source source,
                                                                                         Supplier<Dest> s){
        Dest dest = s.get();
        dest.addAll(source);
        return dest;
    }
}
